from flask import Blueprint, jsonify, request
from view.courses import get_data
import json
from db import db1

bp_ds = Blueprint('ds', __name__)


@bp_ds.route('/dianshang', methods=['POST','GET'])
def dianshang():
    if request.method == 'POST':
        data = get_data()
        print(data)
        sql = 'insert into dianshang(name,price,code,pic) values("%s",%.1f,"%s","%s")' % (
            data['name'], float(data['price']), data['code'], data['pic'])

        db1.update(sql)
        return jsonify({'code': 200})
    elif request.method == 'GET':
        sql = 'select * from dianshang'
        res = db1.findall(sql)
        return jsonify({'code':200,'list':res})

import datetime

@bp_ds.route('/jilu',methods=['POST','GET'])
def jilu():
    if request.method=='POST':
        data = get_data()
        sql = 'select price from dianshang where code = "%s"'%(data['code'])
        res = db1.findone(sql)
        stime = datetime.datetime.now()
        print(data['code'],res['price'],stime)
        sql1 = 'insert into ds_jilu(code,stime,price) values("%s","%s",%.1f)'%(data['code'],stime,float(res['price']))
        db1.update(sql1)
        return jsonify({'code':200})
    elif request.method == 'GET':
        sql = 'select count as value,name,d.price from (select code,count(code) as count from ds_jilu GROUP BY code) as j left join dianshang as d on j.code=d.code'
        res = db1.findall(sql)
        return jsonify({'code':200,'list':res})

